package com.nstar.jinianri.controller
{
	import com.nstar.base.weibo.WeiboControllerTemplate;
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.data.MicroBlogUser;
	import com.sina.microblog.events.MicroBlogEvent;

	public class WeiboController extends WeiboControllerTemplate
	{
		private static var inst:WeiboController;
		
		private var logined:Boolean = false;
		private var _user:MicroBlogUser;//当前登录用户
		
		public function WeiboController()
		{
			super();
		}
		
		override protected function addEventListeners ():void{
			getMicroBlog().addEventListener(MicroBlogEvent.LOGIN_RESULT, microBlogHandler);
			getMicroBlog().addEventListener(MicroBlogEvent.VERIFY_CREDENTIALS_RESULT,microBlogHandler);
		}		
		
		public static function getInstance():WeiboController{
			if(!inst){
				inst = new WeiboController();
			}
			return inst;
		} 
		
		public static function getMicroBlog():MicroBlog{
			return inst.getMicroBlog();
		}
		
		//微博事件处理
		private function microBlogHandler(event:MicroBlogEvent):void{
			switch(event.type){
				case MicroBlogEvent.LOGIN_RESULT:
					trace("login ok");
					logined = true;
					microBlog.verifyCredentials();//验证用户
					break;
				case MicroBlogEvent.VERIFY_CREDENTIALS_RESULT:
					_user = event.result as MicroBlogUser;
					trace("verify credentials ok, user -> "+_user.screenName+"("+_user.id+")");
					break;
			}
		}
		
		public function get user():MicroBlogUser{
			return this._user;
		}
	}
}